package com.stockz.ui.swing.charting.painter.axis;

import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.HashMap;
import java.util.Map;

import com.stockz.core.model.Bar;
import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.util.UIChartingConstant;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.view.StockChartPanelSkin;
import com.stockz.ui.swing.charting.view.StockChartPanelState;

public class InformationAxisPainter{
	
	private static Rectangle rectangle;
	
	public static void repaint(StockChartPanel stockChartPanel){
		if(stockChartPanel != null){
			if(InformationAxisPainter.rectangle != null){
				stockChartPanel.repaint(InformationAxisPainter.rectangle);
			}else{
				stockChartPanel.repaint();
			}
			
		}
	}
	
	
	public static void paint(Graphics2D g,StockChartPanel stockChartPanel,  Rectangle rectangle) {
		InformationAxisPainter.rectangle = rectangle;
		StockChartPanelState state = stockChartPanel.getStockChartPanelState();
		StockChartPanelSkin skin = stockChartPanel.getStockChartPanelSkin();
		g.setColor(skin.getAxisInformationColor());
		g.drawLine(
				rectangle.x, 
				rectangle.y+rectangle.height, 
				rectangle.x + rectangle.width, 
				rectangle.y+rectangle.height);
		
		g.setColor(skin.getTextColor());
		g.setFont(skin.getSmallFont());
		Map<Integer,String> map = getInformation(state,rectangle);
		if(map.size() == 0){
			return;
		}
		int cellWidth = rectangle.width/map.size();
		int Y = rectangle.y + g.getFont().getSize() + (rectangle.height - g.getFont().getSize())/2;
		for(int i = 0; i < map.size(); i++){
			String info = map.get(i);
			if(info != null){
				double infoWidth = g.getFont().getStringBounds(info, g.getFontRenderContext()).getWidth();
				int X = (int)(rectangle.x + i*cellWidth + (cellWidth-infoWidth)/2);
				g.drawString(info, X, Y);
			}
		}
	}
	
	
	
	private static Map<Integer,String> getInformation(StockChartPanelState state, Rectangle rectangle){
		Map<Integer,String> map = new HashMap<Integer,String>();
		
		if(state.getBarList() != null && !state.getBarList().isEmpty()){
			int index = MathUtil.unfitX(rectangle.x, 
					state.getTimeUnitSpace(), 
					state.getEndIndex(), 
					state.getCurrentPoint().x);
			if(index < 0){
				index = 0;
			}else if(index >= state.getBarList().size()){
				index = state.getBarList().size()-1;
			}
			Bar bar = state.getBarList().get(index);
			map.put(0, "OPEN : "+UIChartingConstant.priceNumberFormat.format(bar.getOpen()));
			map.put(1, "HIGH : "+UIChartingConstant.priceNumberFormat.format(bar.getHigh()));
			map.put(2, "LOW : "+UIChartingConstant.priceNumberFormat.format(bar.getLow()));
			map.put(3, "CLOSE : "+UIChartingConstant.priceNumberFormat.format(bar.getClose()));
			map.put(4, "DATE : "+UIChartingConstant.dateFormat.format(bar.getDate()));
			map.put(5, "VOLUME : "+bar.getVolume());
		}
		return map;
	}	
}